package com.nanyang.tms.service.cost.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.cost.CostQueryRequest;
import com.nanyang.tms.api.dto.request.cost.CostUpdateStateRequest;
import com.nanyang.tms.api.dto.request.cost.add.*;
import com.nanyang.tms.api.dto.request.cost.update.*;
import com.nanyang.tms.api.dto.response.cost.*;
import com.nanyang.tms.api.service.cost.CostRpcService;
import com.nanyang.tms.dao.cost.CostInfoDao;
import com.nanyang.tms.domain.cost.*;
import com.nanyang.tms.manager.cost.CostManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CostRpcServiceImpl implements CostRpcService {
    @Autowired
    private CostManager costManager;
    @Autowired
    private CostInfoDao costDao;
    @Override
    public ApiResult<PageBean<CostPageResponse>> listCost(CostQueryRequest param) {
        ApiResult<PageBean<CostPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CostInfoQuery query = new CostInfoQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CostInfoVO> costList = costManager.listCost(query);
            PageBean<CostPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(costList.getTotalItem());
            BeanUtils.copyListProperties(costList.getData(), resultList.getData(), CostPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }

    @SystemLog(module =SystemLog.Module.COST,template="costDisable")
    @Override
    public ApiResult<Boolean> disableState(CostUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CostInfoDO costInfoDO = new  CostInfoDO();
            BeanUtils.copyProperties(param,costInfoDO);
            costInfoDO.setGmtModifiedPin(param.getPin());
            costDao.disableState(costInfoDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COST,template="costEnable")
    @Override
    public ApiResult<Boolean> enableState(CostUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CostInfoDO costInfoDO = new  CostInfoDO();
            BeanUtils.copyProperties(param,costInfoDO);
            costInfoDO.setGmtModifiedPin(param.getPin());
            costDao.enableState(costInfoDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> saveCost(CostBaseAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            CostBaseVO costBaseVO = new  CostBaseVO();
            costBaseVO.setGmtCreatePin(param.getPin());
            costBaseVO.setIp(param.getIp());

            CostInfoVO costInfoVO=new CostInfoVO();
            BeanUtils.copyProperties(param.getCostInfoAddRequest(),costInfoVO);
            costBaseVO.setCostInfoVO(costInfoVO);

            CostPartSchemeVO costPartSchemeVO=new CostPartSchemeVO();
            CostPartSchemeAddRequest costPartSchemeAddRequest = param.getCostPartSchemeAddRequest();
            BeanUtils.copyProperties(costPartSchemeAddRequest,costPartSchemeVO);
            List<CostPartAddRequest> costPartAddRequestList = costPartSchemeAddRequest.getCostPartAddRequestList();
            List<CostPartVO> costPartVOList =new ArrayList<>();
            for (CostPartAddRequest costPartAddRequest : costPartAddRequestList) {
                CostPartVO costPartVO=new CostPartVO();
                BeanUtils.copyProperties(costPartAddRequest,costPartVO);
                List<CostPartCountyVO> costPartCountyVOList=new ArrayList<>();
                List<CostPartCountyAddRequest> costPartCountyAddRequestList = costPartAddRequest.getCostPartCountyAddRequestList();
                for (CostPartCountyAddRequest costPartCountyAddRequest : costPartCountyAddRequestList) {
                    CostPartCountyVO costPartCountyVO=new CostPartCountyVO();
                    BeanUtils.copyProperties(costPartCountyAddRequest,costPartCountyVO);
                    List<CostPartCityVO> costPartCityVOList=new ArrayList<>();
                    List<CostPartCityAddRequest> costPartCityAddRequestList = costPartCountyAddRequest.getCostPartCityAddRequestList();
                    if(CollectionUtil.isNotEmpty(costPartCityAddRequestList)){
                        BeanUtils.copyListProperties(costPartCityAddRequestList,costPartCityVOList,CostPartCityVO.class);
                    }
                    costPartCountyVO.setCostPartCityVOList(costPartCityVOList);
                    List<CostPartZipcodeVO> costPartZipcodeVOList=new ArrayList<>();
                    List<CostPartZipcodeAddRequest> costPartZipcodeAddRequestList = costPartCountyAddRequest.getCostPartZipcodeAddRequestList();
                    if(CollectionUtil.isNotEmpty(costPartZipcodeAddRequestList)){
                        BeanUtils.copyListProperties(costPartZipcodeAddRequestList,costPartZipcodeVOList,CostPartZipcodeVO.class);
                    }
                    costPartCountyVO.setCostPartZipcodeVOList(costPartZipcodeVOList);
                    costPartCountyVOList.add(costPartCountyVO);
                }
                costPartVO.setCostPartCountyVOList(costPartCountyVOList);
                costPartVOList.add(costPartVO);
            }
            costPartSchemeVO.setCostPartVOList(costPartVOList);
            costBaseVO.setCostPartSchemeVO(costPartSchemeVO);

            CostWeightAddRequest costWeightAddRequest = param.getCostWeightAddRequest();
            CostWeightVO costWeightVO=new CostWeightVO();
            BeanUtils.copyProperties(costWeightAddRequest,costWeightVO);
            List<CostWeightRecordVO> costWeightRecordVOList=new ArrayList<>();
            List<CostWeightRecordAddRequest> costWeightRecordAddRequestList = costWeightAddRequest.getCostWeightRecordAddRequestList();
            BeanUtils.copyListProperties(costWeightRecordAddRequestList,costWeightRecordVOList,CostWeightRecordVO.class);
            costWeightVO.setCostWeightRecordVOList(costWeightRecordVOList);
            costBaseVO.setCostWeightVO(costWeightVO);

            List<CostPriceRecordAddRequest> costPriceRecordAddRequestList = param.getCostPriceRecordAddRequestList();
            List<CostPriceRecordVO> costPriceRecordVOList=new ArrayList<>();
            BeanUtils.copyListProperties(costPriceRecordAddRequestList,costPriceRecordVOList,CostPriceRecordVO.class);
            costBaseVO.setCostPriceRecordVOList(costPriceRecordVOList);

            costManager.saveCost(costBaseVO);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateCost(CostBaseUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<Boolean>();
        try {
            param.requestCheck();
            CostBaseVO costBaseVO = new  CostBaseVO();
            costBaseVO.setGmtCreatePin(param.getPin());
            costBaseVO.setIp(param.getIp());

            CostInfoVO costInfoVO=new CostInfoVO();
            BeanUtils.copyProperties(param.getCostInfoUpdateRequest(),costInfoVO);
            costBaseVO.setCostInfoVO(costInfoVO);

            CostPartSchemeVO costPartSchemeVO=new CostPartSchemeVO();
            CostPartSchemeUpdateRequest costPartSchemeUpdateRequest = param.getCostPartSchemeUpdateRequest();
            BeanUtils.copyProperties(costPartSchemeUpdateRequest,costPartSchemeVO);
            List<CostPartUpdateRequest> costPartUpdateRequestList = costPartSchemeUpdateRequest.getCostPartUpdateRequestList();
            List<CostPartVO> costPartVOList =new ArrayList<>();
            for (CostPartUpdateRequest costPartUpdateRequest : costPartUpdateRequestList) {
                CostPartVO costPartVO=new CostPartVO();
                BeanUtils.copyProperties(costPartUpdateRequest,costPartVO);
                List<CostPartCountyVO> costPartCountyVOList=new ArrayList<>();
                List<CostPartCountyUpdateRequest> costPartCountyUpdateRequestList = costPartUpdateRequest.getCostPartCountyUpdateRequestList();
                for (CostPartCountyUpdateRequest costPartCountyUpdateRequest : costPartCountyUpdateRequestList) {
                    CostPartCountyVO costPartCountyVO=new CostPartCountyVO();
                    BeanUtils.copyProperties(costPartCountyUpdateRequest,costPartCountyVO);
                    List<CostPartCityVO> costPartCityVOList=new ArrayList<>();
                    List<CostPartCityUpdateRequest> costPartCityUpdateRequestList = costPartCountyUpdateRequest.getCostPartCityUpdateRequestList();
                    if(CollectionUtil.isNotEmpty(costPartCityUpdateRequestList)){
                        BeanUtils.copyListProperties(costPartCityUpdateRequestList,costPartCityVOList,CostPartCityVO.class);
                    }
                    costPartCountyVO.setCostPartCityVOList(costPartCityVOList);
                    List<CostPartZipcodeVO> costPartZipcodeVOList=new ArrayList<>();
                    List<CostPartZipcodeUpdateRequest> costPartZipcodeUpdateRequestList = costPartCountyUpdateRequest.getCostPartZipcodeUpdateRequestList();
                    if(CollectionUtil.isNotEmpty(costPartZipcodeUpdateRequestList)){
                        BeanUtils.copyListProperties(costPartZipcodeUpdateRequestList,costPartZipcodeVOList,CostPartZipcodeVO.class);
                    }
                    costPartCountyVO.setCostPartZipcodeVOList(costPartZipcodeVOList);
                    costPartCountyVOList.add(costPartCountyVO);
                }
                costPartVO.setCostPartCountyVOList(costPartCountyVOList);
                costPartVOList.add(costPartVO);
            }
            costPartSchemeVO.setCostPartVOList(costPartVOList);
            costBaseVO.setCostPartSchemeVO(costPartSchemeVO);

            CostWeightUpdateRequest costWeightUpdateRequest = param.getCostWeightUpdateRequest();
            CostWeightVO costWeightVO=new CostWeightVO();
            BeanUtils.copyProperties(costWeightUpdateRequest,costWeightVO);
            List<CostWeightRecordVO> costWeightRecordVOList=new ArrayList<>();
            List<CostWeightRecordUpdateRequest> costWeightRecordUpdateRequestList = costWeightUpdateRequest.getCostWeightRecordUpdateRequestList();
            BeanUtils.copyListProperties(costWeightRecordUpdateRequestList,costWeightRecordVOList,CostWeightRecordVO.class);
            costWeightVO.setCostWeightRecordVOList(costWeightRecordVOList);
            costBaseVO.setCostWeightVO(costWeightVO);

            List<CostPriceRecordUpdateRequest> costPriceRecordUpdateRequestList = param.getCostPriceRecordUpdateRequestList();
            List<CostPriceRecordVO> costPriceRecordVOList=new ArrayList<>();
            BeanUtils.copyListProperties(costPriceRecordUpdateRequestList,costPriceRecordVOList,CostPriceRecordVO.class);
            costBaseVO.setCostPriceRecordVOList(costPriceRecordVOList);

            costManager.updateCost(costBaseVO);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<CostBaseResponse> selectByCode(String code) {
        ApiResult<CostBaseResponse> result=new ApiResult<>();
        try {
            CostBaseResponse costBaseResponse=new CostBaseResponse();
            CostInfoResponse costInfoResponse=new CostInfoResponse();
            CostBaseVO costBaseVO = costManager.selectByCode(code);
            BeanUtils.copyProperties(costBaseVO.getCostInfoVO(),costInfoResponse);
            costBaseResponse.setCostInfoResponse(costInfoResponse);

            CostPartSchemeResponse costPartSchemeResponse=new CostPartSchemeResponse();
            CostPartSchemeVO costPartSchemeVO = costBaseVO.getCostPartSchemeVO();
            BeanUtils.copyProperties(costPartSchemeVO,costPartSchemeResponse);
            List<CostPartResponse> costPartResponseList=new ArrayList<>();
            List<CostPartVO> costPartVOList = costPartSchemeVO.getCostPartVOList();
            costPartVOList.forEach(costPartVO->{
                CostPartResponse costPartResponse=new CostPartResponse();
                BeanUtils.copyProperties(costPartVO,costPartResponse);
                List<CostPartCountyResponse> costPartCountyResponseList=new ArrayList<>();
                List<CostPartCountyVO> costPartCountyVOList = costPartVO.getCostPartCountyVOList();
                costPartCountyVOList.forEach(costPartCountyVO ->{
                    CostPartCountyResponse costPartCountyResponse=new CostPartCountyResponse();
                    BeanUtils.copyProperties(costPartCountyVO,costPartCountyResponse);
                    List<CostPartCityResponse> costPartCityResponseList=new ArrayList<>();
                    List<CostPartCityVO> costPartCityVOList = costPartCountyVO.getCostPartCityVOList();
                    if(CollectionUtil.isNotEmpty(costPartCityVOList)){
                        BeanUtils.copyListProperties(costPartCityVOList,costPartCityResponseList,CostPartCityResponse.class);
                    }
                    List<CostPartZipcodeResponse> costPartZipcodeResponseList=new ArrayList<>();
                    List<CostPartZipcodeVO> costPartZipcodeVOList = costPartCountyVO.getCostPartZipcodeVOList();
                    if(CollectionUtil.isNotEmpty(costPartZipcodeVOList)){
                        BeanUtils.copyListProperties(costPartZipcodeVOList,costPartZipcodeResponseList,CostPartZipcodeResponse.class);
                    }
                    costPartCountyResponse.setCostPartZipcodeResponseList(costPartZipcodeResponseList);
                    costPartCountyResponse.setCostPartCityResponseList(costPartCityResponseList);
                    costPartCountyResponseList.add(costPartCountyResponse);
                });
                costPartResponse.setCostPartCountyResponseList(costPartCountyResponseList);
                costPartResponseList.add(costPartResponse);
            });
            costPartSchemeResponse.setCostPartResponseList(costPartResponseList);
            costBaseResponse.setCostPartSchemeResponse(costPartSchemeResponse);

            CostWeightResponse costWeightResponse=new CostWeightResponse();
            CostWeightVO costWeightVO = costBaseVO.getCostWeightVO();
            BeanUtils.copyProperties(costWeightVO,costWeightResponse);
            List<CostWeightRecordResponse> costWeightRecordResponseList=new ArrayList<>();
            List<CostWeightRecordVO> costWeightRecordVOList = costWeightVO.getCostWeightRecordVOList();
            BeanUtils.copyListProperties(costWeightRecordVOList,costWeightRecordResponseList,CostWeightRecordResponse.class);
            costWeightResponse.setCostWeightRecordResponseList(costWeightRecordResponseList);
            costBaseResponse.setCostWeightResponse(costWeightResponse);

            List<CostPriceRecordResponse> costPriceRecordResponseList=new ArrayList<>();
            BeanUtils.copyListProperties(costBaseVO.getCostPriceRecordVOList(),costPriceRecordResponseList,CostPriceRecordResponse.class);
            costBaseResponse.setCostPriceRecordResponseList(costPriceRecordResponseList);

            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(costBaseResponse);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
